<template>
  <div id="contentBody" class="box-body">
    <div class="box box-primary">
      <div class="box-header with-border">
        <h3 class="box-title">参加活动（竞赛）情况</h3>
        <div class="box-tools pull-right">
          <button type="button" v-if='isEditable' class="btn btn btn-success" @click="()=>addVisible = true">
            <i class='fa fa-plus-square-o'>添加参加活动（竞赛）情况</i>
          </button>
        </div>
      </div>
      <div class="box-body">
        <div class=" form-inline dt-bootstrap" id="example1_wrapper">
          <div class="row">
            <div class="col-sm-12 table-responsive">
              <table class="table table-hover table table-bordered table-striped dataTable">
                <tbody>
                  <tr>
                    <th aria-label="活动名称" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">活动名称</th>
                    <th aria-label="活动级别" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">活动级别</th>
                    <th aria-label="时间" style="width: 182px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">时间</th>
                    <th aria-label="地点" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">地点</th>
                    <th aria-label="参与形式" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">参与形式</th>
                    <th aria-label="参与项目（科目）" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">参与项目（科目）</th>
                    <th aria-label="取得成绩" style="width: 181px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">取得成绩</th>
                    <th aria-label="备注" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">备注</th>
                    <th v-if='isEditable' aria-label="操作" style="width: 101px;" colspan="1" rowspan="1" aria-controls="tblresumes" tabindex="0" class="sorting">操作</th>
                  </tr>
                  <tr :key='index' v-for='(value,index) in this.races' role="row" class="value.cls">
                    <td>{{ value.name }}</td>
                    <td class="text-light" v-text="getraccelevelname(value.level)"></td>
                    <td>{{ value.playtime }}</td>
                    <td>{{ value.location }}</td>
                    <td class="text-light" v-text="getracetypename(value.racetype)"></td>
                    <td>{{ value.subject }}</td>
                    <td>{{ value.reward }}</td>
                    <td>{{ value.comment }}</td>
                    <td v-if='isEditable'>
                      <div class="col-md-2">
                        <button @click="deleterace(index)" class="btn btn-xs">删除</button>
                      </div>
                    </td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
      <el-dialog id='raceDlg' title='新增参加活动（竞赛）情况' v-if="addVisible" :visible.sync="addVisible" :before-close="()=>addVisible = false" :modal="false" size="small">
        <div class="form-horizontal">
          <div class="box-body">
            <div class="form-group">
              <label class="col-md-2 control-label my-label">活动名称</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editrace.name" placeholder="请输入活动名称">
              </div>
              <label class="col-md-2 control-label my-label">活动级别</label>
              <div class="col-sm-4">
                <select v-model="editrace.level" class="form-control">
                  <option :key='option.id' v-for="option in this.racelevels" v-bind:value="option.id">{{ option.name }}</option>
                </select>
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">时间</label>
              <div class="col-sm-4">
                <MyDatePicker v-bind:date="editrace.playtime" @timeChanged="time=>editrace.playtime = time"></MyDatePicker>
              </div>
              <label class="col-md-2 control-label my-label">地点</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editrace.location" placeholder="请输入活动地点">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">参与形式</label>
              <div class="col-sm-4">
                <select v-model="editrace.racetype" class="form-control">
                  <option :key='option.id' v-for="option in this.racetypes" v-bind:value="option.id">{{ option.name }}</option>
                </select>
              </div>
              <label class="col-md-2 control-label my-label">参与项目（科目）</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editrace.subject" placeholder="请输入参与项目（科目）">
              </div>
            </div>
            <div class="form-group">
              <label class="col-md-2 control-label my-label">取得成绩</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editrace.reward" placeholder="请输入成绩">
              </div>
              <label class="col-md-2 control-label my-label">备注</label>
              <div class="col-sm-4">
                <input class="form-control" v-model="editrace.comment" placeholder="请输入备注">
              </div>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addVisible = false">关 闭</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import MyDatePicker from '../../mycomponents/common/MyDatePicker'
export default {
  name: 'Race',
  props: ['races','editable'],
  data: function () {
    return {
      isEditable:true,
      addVisible: false,
      editrace: {
        level: 1,
        name: '',
        playtime: '',
        location: '',
        racetype: 1,
        subject: '',
        reward: '',
        comment: ''
      },
      racelevels:[],
      racetypes:[]
    }
  },
  created:function(){
    if(this.editable != null){
      this.isEditable = this.editable
    }
  },
  mounted:function(){
    this.racelevels = this.$store.state.sysconfigs.racelevels
    this.racetypes = this.$store.state.sysconfigs.racetypes
  },
  components: {
    MyDatePicker
  },
  methods: {
    clearEditData() {
      this.editrace.level = 1,
        this.editrace.name = '',
        this.editrace.playtime = '',
        this.editrace.location = '',
        this.editrace.racetype = 1,
        this.editrace.subject = '',
        this.editrace.reward = '',
        this.editrace.comment = ''
    },
    getname(cates, id) {
      for (var index in cates) {
        if (cates[index].id == id) {
          return cates[index].name;
        }
      }
      return ''
    },
    getraccelevelname(id) {
      return this.getname(this.racelevels, id)
    },
    getracetypename(id) {
      return this.getname(this.racetypes, id)
    },
    deleterace(index) {
      this.races.splice(index, 1)
    },
    deepCopy(o, c) {
      var c = c || {}
      for (var i in o) {
        if (typeof o[i] === 'object') {
          if (o[i].constructor === Array) {
            c[i] = []
          } else {
            c[i] = {}
          }
          this.deepCopy(o[i], c[i])
        } else {
          c[i] = o[i]
        }
      }
      return c
    },
    submit(e) {
      var newRace = {}
      this.deepCopy(this.editrace, newRace)
      this.races.push(newRace)
      this.clearEditData()
      this.addVisible = false
    }
  }
}
</script>
<style scoped>
#contentBody{
   min-height: 400px;
}
</style>